
THE_CLOSER:
	mousegetpos,,, win
	wingettitle, title, ahk_id %win%
	wingetclass, class, ahk_id %win%
	list := Trim(USER_INI.TheCloser.CloseTabIfActiveList)
	sleep 100
	if class contains %list%
	{
		Send, {Blind}^{F4}
		return
	}
	if title contains %list%
	{
		Send, {Blind}^{F4}
		return
	}
	
	else
		WinClose, ahk_id %win%
return



ConvertCase:
	mods := GetModifiers()
	CBB := Clipboard
	try selTxt := GetSelection()
	catch e
	{
		msgbox % e
		return
	}
	if (mods = "^+")
	{
		StringUpper, selTxt, selTxt		
	} 
	else if (mods = "^")	
	{
		StringLower, selTxt, selTxt
	}
	else if (mods = "^#")	
	{
		StringUpper, selTxt, selTxt, T
	}
	Clipboard := selTxt
	sleep 30
	Send, {Blind}^v
return
	



CopyTo:
	GetKeyState, keyState, Alt, P
	if (keyState = "D")
	{
		try CopyToRun()
		catch e
		{
			msgbox % e
			return
		}
	}
	else
	{
		try copyToNotepad()
		catch e
		{
			MsgBox % e
			return
		}
	}
return



ScriptManage(cmd)
{
	if cmd = reload
		Reload
	if cmd = suspend
		Suspend
	if cmd = pause
		Pause
	if cmd = exit
		exit
}



runner(THE_WINDOW)
{
	SetTitleMatchMode, 2
	IfWinExist, % THE_WINDOW
		WinActivate, % THE_WINDOW
	else
		Run % THE_WINDOW
	SetTitleMatchMode, 1
}



ACTIVE_OPEN(ACTIVE_WIN, OPEN_FILE)
{
	TMMbu := A_TitleMatchMode
	SetTitleMatchMode, 2
	IfWinActive, %ACTIVE_WIN%
		Run, % OPEN_FILE
	SetTitleMatchMode, %TMMbu%
}




/*!
	Function: IsCloseTabWindow()
		Returns a true/false value based on whether or not the window under the mouse
		exists in the DisableIfActiveList for the 'TheCloser' utility.		
	
	Returns: Returns True if ID matches a window in the window list, or false otherwise
*/
IsCloseTabWindow()
{
	MouseGetPos,,, aHwnd
	closeTabWins := Trim(USER_INI.TheCloser.CloseTabIfActiveList)
	WinGetClass, winClass, ahk_id %aHwnd%
	WinGetTitle, winTitle, ahk_id %aHwnd%
	
	if (InStr(closeTabWins, winClass))
	{
		return 1
	}
	if (InStr(closeTabWins, winTitle))
	{
		return 1
	}
	return
}




/*!
	Function:  GetSelection([PlainText, RestoreCB])
		Get the user's selection and converts it to text format
	Parameters:
			PlainText - (Optional) This flag determines if the return value will be
						treated as plain text or returned in the standard format. Defaults to true &
						returns plain text unless either 0, false or an empty string ("") is specified		
			RestoreCB - (Optional) Flag that sets whether or not to restore the clipboard to its
						original content. Default is 0 (*don't* restore clipboard)
	Returns:
		Returns the selection (as plaintext or default formatting, depending on value
		of the *PlainText* flag passed (default is plain text).
	Throws:
		This function will throw an exception if an error is encountered (i.e. unable
		to copy selection to clipboard)
*/
GetSelection(PlainText = 1, RestoreCB = 0)
{	
	cbBU := ClipboardAll
	Clipboard := ""
	Send, {Blind}^c
	ClipWait, 2, 1
	If (ErrorLevel)
	{
		throw "No clipboard content"
	}
	Selection := PlainText ? Clipboard : ClipboardAll
	if (RestoreCB)
	{
		Clipboard := cbBU
	}
	return, Selection
}



/*!
	Function: EditCode([script])
		Opens the specified (or currently selected) file for editing. 
	Parameters:
		script - (Optional) Full path of the file to edit. If omitted, the currently 
				 selected file path (if a file is selected) will be used.
*/
EditCode(script = "")
{
	if (!script)
	{
		try script := getSelection()
		catch e
		{
			msgbox % "Error...`n`n" e
			return
		}
	}
	run, *edit `"%script%`"
}


/*!
	Function: SaveSelection()
		Saves the user's selection in a txt file
	Throws:
		This function will throw an exception if an error occurs. Errors
		include being unable to get selected text, not having a valid output
		file path, file IO errors and others.
*/
SaveSelection()
{
	try Selection := GetSelection()
	catch e
	{
		throw "Failed to get selected text"
	}
	fileselectfile, Path, S 24,, Where should the file be saved?, Text Files (*.txt)
	If (ErrorLevel || Path = "")
	{
		throw "Invalid output file path."
		return
	}
	try
	{
		FileDelete, % Path
		FileAppend, % Selection, % Path
	}
	catch e
	{
		throw "Error occured during " e.what
	}
		
}



/*!
	Function: CopyToNotepad()
		Copies the selected text into a new Notepad window
*/
CopyToNotepad()
{
	try selText := GetSelection()
	catch e 
	{
		msgbox % e
		return
	}
	noteFilePath := A_Temp "\CopyToNotepad.txt"
	noteFileObj := FileOpen(noteFilePath, "w")	
	if (!IsObject(noteFileObj))
	{
		msgbox Failed to create temp text file in user's temp folder...
		return
	}
	noteFileObj.Write(selText)
	noteFileObj.Close()
	try Run, %noteFilePath%	
	catch
	{
		msgbox Failed to run the created text file...
	}
	noteFileObj := ""
}




/*!
	Function: CopyToRun()
		Copies the current selected text into a Run command window.
*/
CopyToRun()
{
	try Selection := GetSelection()
	catch e 
	{
		msgbox % e
		return
	}
	TTM := A_TitleMatchMode
	SetTitleMatchMode, 3
	Send, {Blind}#r
	WinWait, Run,,1.5
	If (ErrorLevel)
	{
		msgbox Unable to open/find Run prompt.
		return
	}
	Send, {Blind}^v
	SetTitleMatchMode, %TTM%
}




/*!
	Function: GetSciTEpath()
		Returns the full path to SciTE on the current machine.
	Returns:
		Returns the full path to the SciTE4AutoHotkey executable (if found).
		If SciTE isn't found then an empty string is returned.
*/
GetSciTEpath()
{
	ahkDir := Trim(RegExReplace(Trim(A_AhkPath), "i)\\AutoHotkey.exe$"))
	SciTEdir := ""
	Loop, %ahkDir%\*, 1
	{
		if instr(A_LoopFileName, "SciTE")
		{
			FileGetAttrib, attribs, %A_LoopFileFullPath%
			if instr(attribs, "D")
			{
				SciTEdir := A_LoopFileFullPath
				break
			}
		}
	}
	if !(SciTEdir)
		return
	loop, %SciTEdir%\*
		if (instr(A_LoopFileName, "SciTE") && (A_LoopFileExt = "exe"))
			return A_LoopFileFullPath
	return
}






